import * as services from 'services/tongue';
import { Toast } from 'antd-mobile';
import { routerRedux } from 'dva/router';

export default {

  namespace: 'tongue',

  state: {
    list: [],
    detail: {},
    step2: {},
  },

  reducers: {
    save(state, action) {
      return { ...state, ...action.payload };
    },

    filter(state, action) {
      return {
        ...state,
        list: state.list.filter(tongue => tongue.Id !== action.payload),
      };
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      let Tongue  = yield call(services.queryTongueList);
      if (Tongue === '[]' || !Tongue) {
        Tongue = [];
      }
      yield put({ type: 'save', payload: { list: Tongue.data } });
    },

    *query({ payload: {id} }, { call, put }) {
      const Tongue= yield call(services.queryTongueDetail, id);
      yield put({ type: 'save', payload: { detail: Tongue.data } });
    },

    *delete({ payload: {id} }, { call, put }) {
      try {
        yield call(services.deleteTongueById, id);
        Toast.success('删除成功',1.5);
        yield put(
            routerRedux.replace({
               pathname: `/healthy/archives/tongue`,
          })
        );
      } catch(e){
        Toast.fail('删除失败',1.5);
      }
    },
     
    *saveTongue({ payload: {id, patientId, sheSe, bianJianHong, yuBanYuDian, taiSe, houBao, ni, fu, taiBao, pangShou, chiHen, dianCi, lieWen, moistDry, toughTender, comment} }, { call, put}) {
        let res = yield call(services.saveTongue, id, patientId, sheSe, bianJianHong, yuBanYuDian, taiSe, houBao, ni, fu, taiBao, pangShou, chiHen, dianCi, lieWen, moistDry, toughTender, comment);
        if(res.code == 200){
            Toast.success(res.message,1);
            yield put(routerRedux.replace('/healthy/archives/tongue'));
        }else{
            Toast.fail(res.message,1.5);
        }
    },

    *saveTongueImage({ payload: { mediaId, hospitalId, patientId } }, { call, put }) {
      Toast.loading('正在上传图片请稍候...', 6.5);
      hospitalId = JSON.parse(localStorage.getItem("auth"+sessionStorage.getItem("hid"))).hospitalId;
      const res = yield call(services.saveTongueImage, mediaId, hospitalId);
      if (res.code == 200) {
          const step2 = yield call(services.saveTongueStep2, res.data);
          if(step2.code == 200){
            yield put({ type: "save", payload: { step2: step2.data}});
            yield put(
              routerRedux.push({
                pathname: '/healthy/archives/save',
                state: {
                  patientId,
                },
              }),
            );
          }else{
             Toast.fail(step2.message,2);
          }
      }else{
         Toast.fail(res.message,2);
      }
    },
  },

  subscriptions: {// 异步数据初始化
    setup({ dispatch, history }) {
      return history.listen(({ pathname, query, ...rest }) => {
        if (/archives\/tongue$/.test(pathname)) {
          dispatch({ type: 'fetch' });
        }
        if (/tongue\/\d+$/.test(pathname)) {
          dispatch({ type: 'query', payload: {id:rest.state.id} });
        }
      });
    },
  },
}
